package com.diamond.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import com.diamond.bo.DiamondBO;
import com.diamond.entity.Diamond;
import com.diamond.po.DiamondPO;
import com.diamond.service.IDiamondService;

/**
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked")
public class DiamondService extends Service<DiamondPO> implements IDiamondService {

	@Override
	public Diamond findByBatch(String parameter) {
		if(parameter==null||parameter.equals(""))return null;
		String HQL="from Diamond where batch=?";
		List list = genericDAO.find(HQL,new Object[]{parameter});
		if(list!=null&&list.size()>0)return (Diamond) list.get(0);
		return null;
	}

	public void saveOrUpdate(List<DiamondBO> list, String uploadType) throws Exception {
		if ("A".equals(uploadType)) {
			String delSql = "DELETE FROM diamond";
			super.genericDAO.updateBySql(delSql);
			for (DiamondBO diamondBo : list) {
				Diamond diamond = new Diamond();
				PropertyUtils.copyProperties(diamond, diamondBo);
				List<Diamond> diamondlist = genericDAO.findByHQL("from Diamond where reportNo='" + diamond.getReportNo() + "'");
				if (diamondlist.size()>0) {
					for(Diamond dia : diamondlist) {
						if (dia.getSupPrice() < diamond.getSupPrice()) {
							diamond.setBatch(dia.getBatch());
							diamond.setLab(dia.getLab());
							diamond.setSupplierName(dia.getSupplierName());
							diamond.setSupPrice(dia.getSupPrice());
						}
					}
					super.genericDAO.updateBySql("delete from diamond where report_no='"+diamond.getReportNo() + "'");
				}
				super.genericDAO.save(diamond);
			}
		} else if ("B".equals(uploadType)) {
//			StringBuilder delSqlByBatch = new StringBuilder("delete from diamond where batch in (");
//			int i = 0;
//			for (DiamondBO delDiamondBo : list) {
//				if (i+1 < list.size()) { 
//					delSqlByBatch.append("'" + delDiamondBo.getBatch() + "',");
//				} else {
//					delSqlByBatch.append("'" + delDiamondBo.getBatch() + "')");
//				}
//				i++;
//			}
//			super.genericDAO.updateBySql(delSqlByBatch.toString());
			for (DiamondBO diamondBo : list) {
				Diamond diamond = new Diamond();
				PropertyUtils.copyProperties(diamond, diamondBo);
				List<Diamond> diamondlist = genericDAO.findByHQL("from Diamond where reportNo='" + diamond.getReportNo() + "'");
				if (diamondlist.size()>0) {
					for(Diamond dia : diamondlist) {
						if (dia.getSupPrice() < diamond.getSupPrice()) {
							diamond.setBatch(dia.getBatch());
							diamond.setLab(dia.getLab());
							diamond.setSupplierName(dia.getSupplierName());
							diamond.setSupPrice(dia.getSupPrice());
						}
					}
					super.genericDAO.updateBySql("delete from diamond where report_no='"+diamond.getReportNo() + "'");
				}
				super.genericDAO.save(diamond);
			}
		}
	}
	
	
	public List<Diamond> findAll() {
		List<Diamond> list = genericDAO.findByHQL("from Diamond");
		return list;
	}
}
